<?php
require_once 'MyPdoPublic.class.php';
class TablePublicCity extends MyPdoPublic{
	private $tableName = 'public_city';

	//更改开通城市信息
	public function _updateOpenCity($host,$name,$county_code){
		$sql = "update {$this->tableName} set db_host=?,db_name=? where code = ?";
		return $this->dml($sql,array($host,$name,$county_code));
	}




	//获取全部城市数据
	public function _getCityInfoFull(){
		$sql = "select id,code,name,parent_id,level,db_host,db_name from {$this->tableName}";
		$res = $this->dql($sql,array());
		if($res){
			$data = $res;
		}else{
			$data = array();
		}
		return $data;
	}




	/*
		### 获取已开通的城市列表
		反回省市县三个数组
	*/
	public function _getOpenCityInfo(){
		$sql = "select id,code,name,parent_id,level,db_host,db_name from {$this->tableName} where level < 2 or level = 2 and length(db_host) > 0 and length(db_name) > 0";
		$res = $this->dql($sql,array());
		if($res){
			$CityInfo = $res;
			$Province_Arr = $City_Arr = $County_Arr = [];	//用来装载省市县分类后的城市数据
			$Province_Arr2 = $City_Arr2 = $County_Arr2 = [];//用来装载筛选后的城市数据

			// ### 归集省市县数据，同时提取被选取县所属的市id列表
			$City_ids = [];	//临时存放市id
			for($i=0;$i<count($CityInfo);$i++){
				//过滤掉港奥台
				if($CityInfo[$i]['level'] == 0){
					if($CityInfo[$i]['code'] == 710000 || $CityInfo[$i]['code'] == 810000 || $CityInfo[$i]['code'] == 820000){
						continue;
					}
				}
				if($CityInfo[$i]['level'] == 0){			//归集省数据
					$Province_Arr2[] = $CityInfo[$i];
				}
				if($CityInfo[$i]['level'] == 1){			//归集市数据
					$City_Arr2[] = $CityInfo[$i];
				}
				if($CityInfo[$i]['level'] == 2){			//归集县数据
					$County_Arr[] = $CityInfo[$i];
					if(!in_array($CityInfo[$i]['parent_id'],$City_ids)){
						$City_ids[] = intVal($CityInfo[$i]['parent_id']);
					}
				}
			}

			// ### 筛选市数据
			$province_ids = [];//临时存放省id
			for($i=0;$i<count($City_Arr2);$i++){
				for($j=0;$j<count($City_ids);$j++){
					if(intVal($City_ids[$j]) == intVal($City_Arr2[$i]['id'])){
						$City_Arr[] = $City_Arr2[$i];
						if(!in_array($City_Arr2[$i]['parent_id'],$province_ids)){
							$province_ids[] = intVal($City_Arr2[$i]['parent_id']);
						}
					}
				}
			}


			//筛选省数据
			for($i=0;$i<count($Province_Arr2);$i++){
				for($j=0;$j<count($province_ids);$j++){
					if(intVal($province_ids[$j]) == intVal($Province_Arr2[$i]['id'])){
						$Province_Arr[] = $Province_Arr2[$i];
					}
				}
			}

			$data = array(
				'Province_Arr'=>$Province_Arr,
				'City_Arr'=>$City_Arr,
				'County_Arr'=>$County_Arr
			);
		}else{
			$data = array();
		}
		return $data;
	}




	/*
		### 获取已开通的城市列表并将数据处理 （适用小程序前端使用）
		反回 省包市 市包县的数据
	*/
	public function _getOpenCityInfo2(){
		$sql = "select id,code,name,parent_id,level,db_host,db_name from {$this->tableName} where level < 2 or level = 2 and length(db_host) > 0 and length(db_name) > 0";
		$res = $this->dql($sql,array());

	
		$Province_Arr = $City_Arr = $County_Arr = [];	//用来装载省市县分类后的城市数据
		$Province_Arr2 = $City_Arr2 = $County_Arr2 = [];//用来装载筛选后的城市数据

		if($res){
			$CityInfo = $res;

			// ### 归集省市县数据，同时提取被选取县所属的市id列表
			$City_ids = [];	//临时存放市id
			for($i=0;$i<count($CityInfo);$i++){
				//过滤掉港奥台
				if($CityInfo[$i]['level'] == 0){
					if($CityInfo[$i]['code'] == 710000 || $CityInfo[$i]['code'] == 810000 || $CityInfo[$i]['code'] == 820000){
						continue;
					}
				}
				if($CityInfo[$i]['level'] == 0){			//归集省数据
					$Province_Arr2[] = $CityInfo[$i];
				}
				if($CityInfo[$i]['level'] == 1){			//归集市数据
					$City_Arr2[] = $CityInfo[$i];
				}
				if($CityInfo[$i]['level'] == 2){			//归集县数据
					$County_Arr[] = $CityInfo[$i];
					if(!in_array($CityInfo[$i]['parent_id'],$City_ids)){
						$City_ids[] = intVal($CityInfo[$i]['parent_id']);
					}
				}
			}

			// ### 筛选市数据
			$province_ids = [];//临时存放省id
			for($i=0;$i<count($City_Arr2);$i++){
				for($j=0;$j<count($City_ids);$j++){
					if(intVal($City_ids[$j]) == intVal($City_Arr2[$i]['id'])){
						$City_Arr[] = $City_Arr2[$i];
						if(!in_array($City_Arr2[$i]['parent_id'],$province_ids)){
							$province_ids[] = intVal($City_Arr2[$i]['parent_id']);
						}
					}
				}
			}


			//筛选省数据
			for($i=0;$i<count($Province_Arr2);$i++){
				for($j=0;$j<count($province_ids);$j++){
					if(intVal($province_ids[$j]) == intVal($Province_Arr2[$i]['id'])){
						$Province_Arr[] = $Province_Arr2[$i];
					}
				}
			}
		}

		if(@count($County_Arr) > 0 && @count($City_Arr) > 0 && @count($Province_Arr) > 0){
			//return $data;

			//将县归集到市
			foreach($City_Arr as $kCity=>$vCity){
				foreach($County_Arr as $vCounty){
					if($vCounty['parent_id'] == $vCity['id']){
						$City_Arr[$kCity]['county'][] = $vCounty;
					}
				}
			}

			//将市归集到省
			foreach($Province_Arr as $kPro=>$vPro){
				foreach($City_Arr as $vCity){
					if($vCity['parent_id'] == $vPro['id']){
						$Province_Arr[$kPro]['city'][] = $vCity;
					}
				}
			}
			return $Province_Arr;
		}
		return array();
		//dump($Province_Arr,1);
	}


}